package httpd

import (
	"net"
)

type Task struct {
	Client *net.Conn
}

type ThreadPool struct {
	Sender  chan Task
	Control chan int32
	Num     int32
	Dojob   func(*Task)
}

func Worker(control chan int32, boss chan Task, do func(*Task)) {
	for {
		select {
		case <-control:
			return
		case task := <-boss:
			do(&task)
		}
	}
}

func NewThreadPool(num int32, from chan Task, dojob func(*Task)) *ThreadPool {
	ch := make(chan int32, num)
	ret := ThreadPool{
		Num:     num,
		Sender:  from,
		Dojob:   dojob,
		Control: ch,
	}
	return &ret
}

func (tp *ThreadPool) Start() {
	for i := int32(0); i < tp.Num; i++ {
		go Worker(tp.Control, tp.Sender, tp.Dojob)
	}
}
